package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.HobbyDict;
import com.ghck.server.model.query.HobbyDictQuery;
/**
 * 爱好  <br>
 * dict_hobby <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.HobbyDictMapper
 */
 public class HobbyDictProvider {
 
 	/**
 	* 新增 爱好
 	*/
 	public String insert(HobbyDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into dict_hobby(create_time,last_modify_time");
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getSeq()!=null){
			sb.append(",seq");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getSeq()!=null){
			sb.append(",#{seq}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 爱好
 	*/
	public String update(HobbyDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update dict_hobby set last_modify_time = now() ");
		
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		if(domain.getSeq()!=null){
			sb.append(",seq=#{seq}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<HobbyDict>> map){
		StringBuffer sb = new StringBuffer();
		List<HobbyDict> hobbyDicts = map.get("list");
		sb.append("insert into dict_hobby(create_time,last_modify_time,name,in_used,seq) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].seq}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(hobbyDicts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  hobbyDicts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 爱好
 	*/
	public String findByQuery(HobbyDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from dict_hobby a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(HobbyDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from dict_hobby a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 爱好
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from dict_hobby where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	